package org.hagopa.games.moneypool.msgs.ContentTypes.game;

import java.nio.ByteBuffer;

import java.nio.charset.Charset;
import org.hagopa.games.moneypool.msgs.IMsgContent;

public class Ustatus implements IMsgContent{
	private int playerId = 0; //acid
        private int playerPosition = 0;
	private int playerCash = 0;
        private int aplayerNameSZ = 0;
	private String activePlayerName = "";
	
	
	
	private int total_size = 40;	// preset size (before knowing Name)

	//|acid|new_position|cash_status|SizeOfName|Name|abid|upgrade_level|curr_cost|fee|status|owner|
        
	public byte[] marshallToByte() {
		ByteBuffer dataArray = ByteBuffer.allocate(total_size + 32);
		//total_size =  4*9 + 4;  // already inited in declaration!
                
		byte [] bytes = new byte[total_size];
		//dataArray.putInt(CommandsEnum.UPD_PLAYER.idx());
		dataArray.putInt(this.playerId);
		dataArray.putInt(this.playerPosition);
		dataArray.putInt(this.playerCash);
		dataArray.putInt(this.aplayerNameSZ);
		dataArray.put(this.activePlayerName.getBytes(Charset.forName("UTF-8")));
		System.out.println("[Ustatus](marshallToByte)aplayerNameSZ:" + this.aplayerNameSZ + "playerName string-length:" + this.activePlayerName.length());
		dataArray.rewind();
		dataArray.get(bytes,0,total_size);
		return bytes;
	}
	
	public boolean unmarshallFromByte(byte[] content) {
		ByteBuffer dataArray = ByteBuffer.wrap(content);
		if(null == dataArray)
		{
			return false;
		}
		dataArray.clear();
		
		this.playerId = dataArray.getInt();
		this.playerPosition = dataArray.getInt();
		this.playerCash = dataArray.getInt();
		this.aplayerNameSZ = dataArray.getInt();
		
		return true;
	}

	public int getPlayerId() {
		return playerId;
	}

	public void setPlayerId(int playerId) {
		this.playerId = playerId;
	}

	public int getPlayerCash() {
		return playerCash;
	}

	public void setPlayerCash(int playerCash) {
		this.playerCash = playerCash;
	}

	public int getPlayerPosition() {
		return playerPosition;
	}

	public void setPlayerPosition(int playerPosition) {
		this.playerPosition = playerPosition;
	}
	
	public int getTotal_size() {
		return total_size;
	}

	@Override
	public int getSize() {
		return total_size;
	}

	public String getPlayerName() {
		return activePlayerName;
	}

	public void setPlayerName(String activePlayerName) {
		this.activePlayerName = activePlayerName;
		this.aplayerNameSZ = this.activePlayerName.getBytes(Charset.forName("UTF-8")).length;
		this.total_size += this.aplayerNameSZ;
	}

	public int getPlayerNameSZ() {
		return aplayerNameSZ;
	}

	public void setPlayerNameSZ(int aplayerNameSZ) {
		this.aplayerNameSZ = aplayerNameSZ;
	}
        public void showStatus()
        {
            System.out.println("Status Record For PlayerId:" + this.playerId);
            System.out.println("------------------------------------");
            System.out.println("PlayerName:" + this.activePlayerName);
            System.out.println("PlayerName size:" + this.aplayerNameSZ);
            System.out.println("Player's Cash:" + this.playerCash);
            System.out.println("Current position of Player:" + this.playerPosition);
            System.out.println("------------------------------------");
        }

    public String getConcatContent() {
        String outstr = null;
            outstr = String.valueOf(this.playerId) + String.valueOf(this.playerPosition)+ String.valueOf(this.playerCash)
                    + String.valueOf(this.aplayerNameSZ) + this.activePlayerName;
            return outstr;
    }
}
